﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using PlanningGIS.Util.IO;

namespace PlanningGIS.Util.Drawing
{
    /// <summary>
    /// 生成缩略图
    /// 作者  2015.11.18
    /// </summary>
    public class ThumbnailMaker
    {
        /// <summary>
        /// 通过定宽来生成缩略图
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="width">定宽</param>
        /// <returns>Image对象</returns>
        public static System.Drawing.Image GetThumbnailByWidth(Image originalImage, int width)
        {
            if (originalImage == null) return null;

            int x = 0;
            int y = 0;
            int targetWidth = width;
            int originalWidth = originalImage.Width;
            int originalHeight = originalImage.Height;
            //根据原图的宽度、高度以及定宽来计算等比缩略图的高度
            int targetHeight = originalHeight * width / originalWidth;

            Image bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
            Graphics g = Graphics.FromImage(bitmap);
            //设置质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            try
            {
                g.DrawImage(originalImage, new Rectangle(0, 0, targetWidth, targetHeight),
                new Rectangle(x, y, originalWidth, originalHeight),
                GraphicsUnit.Pixel);
                originalImage.Dispose();
                return bitmap;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 通过定高来生成缩略图
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="height">定高</param>
        /// <returns>Image对象</returns>
        public static System.Drawing.Image GetThumbnailByHeight(Image originalImage, int height)
        {
            if (originalImage == null) return null;

            int x = 0;
            int y = 0;
            int targetHeight = height;
            int originalWidth = originalImage.Width;
            int originalHeight = originalImage.Height;
            //根据原图的宽度、高度以及定宽来计算等比缩略图的高度
            int targetWidth = originalWidth * height / originalHeight;

            Image bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
            Graphics g = Graphics.FromImage(bitmap);
            //设置质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            try
            {
                g.DrawImage(originalImage, new Rectangle(0, 0, targetWidth, targetHeight),
                new Rectangle(x, y, originalWidth, originalHeight),
                GraphicsUnit.Pixel);
                originalImage.Dispose();
                return bitmap;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }


        /// <summary>
        /// 通过设定宽高来裁剪图片
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="width">定宽</param>
        /// <param name="height">定高</param>
        /// <returns>Image对象</returns>
        public static System.Drawing.Image GetThumbnailByHeightAndWidth(Image originalImage, int width, int height)
        {
            if (originalImage == null) return null;

            int x = 0;
            int y = 0;
            int targetWidth = width;
            int targetHeight = height;
            int originalWidth = originalImage.Width;
            int originalHeight = originalImage.Height;


            if ((double)originalWidth / (double)originalHeight > (double)targetWidth / (double)targetHeight)
            {
                originalHeight = originalImage.Height;
                originalWidth = originalImage.Height * targetWidth / targetHeight;
                y = 0;
                x = (originalImage.Width - originalWidth) / 2;
            }
            else
            {
                originalWidth = originalImage.Width;
                originalHeight = originalImage.Width * height / targetWidth;
                x = 0;
                y = (originalImage.Height - originalHeight) / 2;
            }
            
            Image bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
            Graphics g = Graphics.FromImage(bitmap);
            //设置质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            g.DrawImage(originalImage, new Rectangle(0, 0, targetWidth, targetHeight),
               new Rectangle(x, y, originalWidth, originalHeight),
               GraphicsUnit.Pixel);
            try
            {
                g.DrawImage(originalImage, new Rectangle(0, 0, targetWidth, targetHeight),
                new Rectangle(x, y, originalWidth, originalHeight),
                GraphicsUnit.Pixel);
                originalImage.Dispose();
                g.Dispose();
                return bitmap;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 通过比例系数来缩放图片
        /// </summary>
        /// <param name="originalImage">原图</param>
        /// <param name="ratio">缩放系数</param>
        /// <returns>Image对象</returns>
        public static System.Drawing.Image GetThumbnailByRatio(Image originalImage, int ratio)
        {
            if (originalImage == null) return null;

            int originalWidth = originalImage.Width;
            int originalHeight = originalImage.Height;
            int targetWidth = (originalWidth * ratio) / 100;
            int targetHeight = (originalHeight * ratio) / 100;

            Image bitmap = new System.Drawing.Bitmap(targetWidth, targetHeight);
            Graphics g = Graphics.FromImage(bitmap);
            //设置质量插值法
            g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            //设置高质量,低速度呈现平滑程度
            g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            //清空画布并以透明背景色填充
            g.Clear(Color.Transparent);
            try
            {
                g.DrawImage(originalImage, new Rectangle(0, 0, targetWidth, targetHeight),
                new Rectangle(0, 0, originalWidth, originalHeight),
                GraphicsUnit.Pixel);
                originalImage.Dispose();
                return bitmap;
            }
            catch (System.Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// 原图是否要缩略(图片小于等于300KB,不生成缩略图)
        /// </summary>
        /// <param name="originalPath">原图路径</param>
        /// <returns>true:缩略;false:不缩略</returns>
        public static bool NeedThumbnail(string originalPath)
        {
            if (!System.IO.File.Exists(originalPath)) return false;

            int size_KB = FileHelper.GetFileSize_KB(originalPath);
            if (size_KB <= 300)
                return false;
            return true;
        }

        /// <summary>
        /// 原图是否要缩略(图片小于等于300KB,不生成缩略图)
        /// </summary>
        /// <param name="size">图片</param>
        /// <returns>true:缩略;false:不缩略</returns>
        public static bool NeedThumbnail(long size)
        {
            if (size <= 300) return false;
            return true;
        }
    }
}
